import config from "../../config";



export class http {
    /**
     * 数据上传
     * @param params
     * @returns {Promise<unknown>}
     */
    request(params){
        return new Promise((resolve,reject) =>{
            wx.request({
                url: config._URL_ + params.url,
                method: params.method || 'GET',
                data: params.data || null,
                header: {...config.version, ...params.header},
                dataType: 'json',
                success: res => {
                    if(res.data.message && res.data.message.indexOf('用户验证失败或未登录') != -1) {
                        wx.showToast({
                          title: '登录失效，请重新登录。',
                          icon: 'none'
                        })
                        wx.clearStorageSync()
                        return false
                    }
                    resolve(res)
                },
                fail: err => {
                    reject(err)
                }
            })
        })
    }

    /**
     * 上传图片
     * @param params
     * @returns {Promise<unknown>}
     */
    uploadImg(params){
        return new Promise((resolve, reject) => {
            wx.uploadFile({
                url: config._URL_ + params.url,
                filePath: params.filePath,
                name: params.name,
                formData: params.data,
                header: {...config.version, ...params.header},
                success (res){
                    let data = JSON.parse(res.data);
                    if(data.message.indexOf('用户验证失败或未登录') != -1) {
                        wx.showToast({
                          title: '登录失效，请重新登录。',
                          icon: 'none'
                        })
                        wx.clearStorageSync()
                        return false
                    }
                    resolve(res);
                },
                fail(err){
                    
                    reject(err);
                }
            })
        })
    }

}
